﻿{% if RequiresClientFunctions -%}
{% if Framework.IsAngular -%}
function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): Observable<any> {
{%     if WrapDtoExceptions -%}
    return {{ Framework.RxJs.ObservableThrowMethod }}(new {{ ExceptionClassName }}(message, status, response, headers, result));
{%     else -%}
    if (result !== null && result !== undefined)
        return {{ Framework.RxJs.ObservableThrowMethod }}(result);
    else
        return {{ Framework.RxJs.ObservableThrowMethod }}(new {{ ExceptionClassName }}(message, status, response, headers, null));
{%     endif -%}
}

{% elseif Framework.IsAngularJS -%}
function throwException(q: ng.IQService, message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): ng.IPromise<any> {
{%     if WrapDtoExceptions -%}
    return q.reject(new {{ ExceptionClassName }}(message, status, response, headers, result));
{%     else -%}
    if (result !== null && result !== undefined)
        return q.reject(result);
    else
        return q.reject(new {{ ExceptionClassName }}(message, status, response, headers, null));
{%     endif -%}
}

{% else -%}
function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
{%     if WrapDtoExceptions -%}
    throw new {{ ExceptionClassName }}(message, status, response, headers, result);
{%     else -%}
    if (result !== null && result !== undefined)
        throw result;
    else
        throw new {{ ExceptionClassName }}(message, status, response, headers, null);
{%     endif -%}
}

{% endif -%}
{% if Framework.IsAngular -%}
function blobToText(blob: any): Observable<string> {
    return new Observable<string>((observer: any) => {
        if (!blob) {
            observer.next("");
            observer.complete();
        } else {
            let reader = new FileReader(); 
            reader.onload = event => { 
                observer.next((<any>event.target).result);
                observer.complete();
            };
            reader.readAsText(blob); 
        }
    });
}

{% elseif Framework.IsAngularJS -%}
function blobToText(blob: Blob, q: ng.IQService): ng.IPromise<string> {
    return new q((resolve) => { 
        let reader = new FileReader(); 
        reader.onload = event => resolve((<any>event.target).result);
        reader.readAsText(blob); 
    });
}

{% endif -%}
{% endif -%}